package ru.royalgatemagazine.afisha;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public final class PageActivity extends TabActivity implements OnTabChangeListener {
	
	private TabActivity mActivityRef;
	
	private static final int[] sTitles = new int[] { 
			R.string.tab_page_title_1, 
			R.string.tab_page_title_2, 
	};
	
	private static final int[] sIcons = new int[] { 
			android.R.drawable.ic_menu_mapmode,
			android.R.drawable.ic_menu_info_details, 
	};
	
	private static final Intent[] mIntents = new Intent[2];
	
	private void initIntents(){
		mIntents[0] = new Intent(this, CatalogActivity.class);
		mIntents[1] = new Intent(this, AboutActivity.class);
	}
	
	private TabHost mTabHost;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActivityRef = this;
		initIntents();
		setContentView(R.layout.tab_bar);
		mTabHost = getTabHost();
		createTabs();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		mTabHost.setCurrentTab(0);
		mTabHost.clearAllTabs();
		createTabs();
	}

	private void createTabs() {
		mTabHost.getTabWidget().setDividerDrawable(R.drawable.base_tab_sep);
		int currentTab = 0;
		int count = sTitles.length;
		for (int i = 0; i < count; i++) {
			String title = getString(sTitles[i]);
			int icon = sIcons[i];
			Intent intent = mIntents[i];
			setupTab(this, i, title, icon, intent);
		}
		mTabHost.setOnTabChangedListener(this);
		mTabHost.setCurrentTab(currentTab);
	}

	private void setupTab(Context context, int index, String text, int drawable, Intent intent) {
		int layoutId = R.layout.tab;
		View tabView = LayoutInflater.from(context).inflate(layoutId, null);
//		ImageView iv = (ImageView) tabView.findViewById(R.id.tabsImage);
		TextView tv = (TextView) tabView.findViewById(R.id.tabsText);
//		iv.setBackgroundResource(drawable);
		tv.setText(text);
		TabSpec tabSpec = mTabHost.newTabSpec(String.valueOf(index)).setIndicator(tabView).setContent(intent);
		mTabHost.addTab(tabSpec);
	}

	@Override
	public void onTabChanged(String tabId) {
	}
}
